LoginSignup
0
1

【jishaku】discord botでpythonのコマンドを実行する方法

Last updated at Posted at 2022-05-28

アカウントを移行しました。最新の記事は以下から確認できます。
https://zenn.dev/sonyakun/articles/jishaku_discord_bot

今回は、discord.pyのbotで、pythonのコマンドやコードを動かす方法を紹介していきます! 

おそらくスラッシュコマンドでは動作しません。

では、まず、以下のコマンドを実行してください。
pip install jishaku
できない場合は
py -m pip install jishaku
を実行してください。
discord.pyは以下のコマンドで入れられます。
py -m pip install discord.py
そしたら、メモ帳などを開いてください。(僕は今VScode持ってないのでIDLEを使います。)

botを作成する方法

https://discord.com/developers/applications
上のサイトにアクセスします。
こんな画面になると思うので、New Applicationをクリックします。

そしたら、こんな画面が出てくるので、
NAMEにbot名を入力して、Createをクリックします。


botというところをクリックして、右側の画面にボタンが出てくるので、それを押します。

そしたら、上の画面のようになるので、Reset tokenを押します。
そして出てきた文字列をコピーします。

作っていく

そしたら、以下のコードを入力します。

import discord
from discord.ext import commands
from jishaku.features.python import PythonFeature
from jishaku.features.root_command import RootCommand

bot = commands.Bot(command_prefix='好きなプレフィクス', help_command=None) #help_command=Noneは標準のヘルプコマンドを削除する引数です。お好みに調整してください。
TOKEN = 'ここにアクセストークン' #discordbotのログインに必要です

class MyBot(commands.Bot):
    async def is_owner(self, user: discord.User):
        if something:  
            return True
#jishakuは家の扉を開けているようなものなので、botの所有者のみが触れるようにします。
        return await super().is_owner(user)

@bot.event
async def on_ready(): # 起動したらコンソールにログイン通知が表示されます
    print("-----------")
    print("起動しました")
    print("-----------")

@bot.command()
async def loadjsk(ctx): #jishakuをロードするコマンド
    yourID = あなたのID 
    if ctx.message.author.id == yourID or ctx.message.author.id == friendID:
        bot.load_extension('jishaku')
        print("success")
        print("https://github.com/Gorialis/jishaku")
        await ctx.send("📩`jishaku`")

bot.run(TOKEN) ## Botの起動とDiscordサーバーへの接続

##コードの解説

import discord #discord.pyを読み込む
from discord.ext import commands #プレフィクスなどを利用可能にする
from jishaku.features.python import PythonFeature #jishakuを読み込む
from jishaku.features.root_command import RootCommand #jishakuを読み込む
class MyBot(commands.Bot):
    async def is_owner(self, user: discord.User):
        if something:  
            return True

        return await super().is_owner(user)
#このクラスを読み込むことでbot作成者のみがjishakuを利用できるようになる(推奨)
@bot.command()
async def loadjsk(ctx):
    yourID = あなたのID
    if ctx.message.author.id == yourID or ctx.message.author.id == friendID:
        bot.load_extension('jishaku')
        print("success")
        print("https://github.com/Gorialis/jishaku")
        await ctx.send("📩`jishaku`") #jishakuを読み込んだことを知らせるメッセージ
@bot.event
async def on_ready():
    print("-----------")
    print("起動しました")
    print("-----------")
#起動メッセージ

##jishakuの使い方
まず、jishakuをロードします。

#コマンド
簡単な計算や

エラー表示も

printももちろん実行できます。


アンロード(プレフィクスがmc!のばあい)は、
mc!jsk unload jishaku
でできます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1