从运行它的终端/命令行控制discord.py bot

时间:2017-11-30 02:52:09

标签: python-3.x python-asyncio discord.py

我已经浏览了documentation,我无法以任何方式找到从终端/命令行/ etc控制机器人的方法。

例如,如果我在机器人上有一个flip命令,它会选择0或1并分别发送头或尾的消息,我希望能够输入{{1}进入终端并让它将相应的消息发送到#general。

有没有办法实现这一目标?

3 个答案:

答案 0 :(得分:0)

如果要从终端控制机器人,请尝试以下操作:

import discord, random
from discord.ext import commands


min = 0
max = 1


channel = client.get_channel(123456789) # This will be the channel id


terminal_cmd = input('')

if terminal_cmd == 'flip':
    channel.send(random.randint(min, max))

我很确定这是可行的,如果不尝试通过堆栈溢出或github进行侦察以获得答案。在搜索中也尝试使用其他措词。

答案 1 :(得分:0)

@Kai代码无效。 试试这个吧。

import discord
import random
from discord.ext import commands

client = commands.Bot(command_prefix="prefix")
token = "token"

@client.event
async def on_ready():
    while not client.is_closed():
        c = input("text/command")
        if "yourcommand" in c:
            command = client.get_command("yourcommand")
            await command()

@client.command()
async def yourcommand():
    channel = client.get_channel(CHANNEL_ID)
    chioce = ["0", "1"]
    content = random.choice(chioce)
    await channel.send(content)

client.run(token)

答案 2 :(得分:0)

使用 input()命令执行此操作。

这是我为您编写的脚本。我试图解释几乎每一行(“#”之后的句子,这样您可以更好地理解它,以后可以删除它。)

import discord
from discord.ext import commands
import random
from random import randrange
import time
import asyncio

client = commands.Bot(command_prefix = ":")
Token = "XXXXXXX" #your token


@client.event
async def on_ready():
    while True: #loop (running your bot non-stop, so you just have to write "flip" in terminal to run it)
        print("Write 'flip' to flip the coin.")
        command = input() #asking for "flip" command (runs only if you write "flip")
        
        number = randrange(2) #picking heads or tails randomly and printing it

        if command == "flip":
            if number == 0:
                a = "heads"
            else:
                a = "tails"

            print("It's {}!" .format(a)) #prints what it picked

            channel1 = client.get_channel(1234567890) #id of #general >>> right click on channel and click "Copy ID" (you need to have developer mode on).
            await channel1.send("It's {}!" .format(a)) #your message
            await asyncio.sleep(2) #timeout for 2 seconds so your terminal won't look like spaghetti
        else:
            print("Wrong command") #printing "wrong command" if you wrote something else than "flip"
            await asyncio.sleep(2) #also timeout


client.run(Token) #running your script