我没有此功能的任何代码,但我有一个我可以在其中实现此代码的机器人。我是编程新手。
import discord
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('my token is here')
答案 0 :(得分:1)
作为新程序员,我建议您将问题分解为逻辑步骤,并尝试分别解决它们。您还应该准确定义所有内容,包括所有假设。
一旦您弄清楚了这一点,对单个部分进行攻击就容易一些。这是您要阅读文档的地方。
在开始任何这之前,我建议您先阅读一下编程概念的概述。具体来说:
总体系统设计
discord机器人的行为类似于不和谐用户,因此可以一次连接到多个服务器(行会)和渠道。我们通过API使用的事件会向您发送一条消息,该消息可能来自任何行会或渠道。
单人版Verison
以下是您需要回答此问题的文档:
https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token
https://discordapp.com/developers/docs/intro
https://discordpy.readthedocs.io/en/latest/api.html#message
Aaaaaa,这是代码。
import discord
import random
client = discord.Client()
players = dict()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
# Check https://discordpy.readthedocs.io/en/latest/api.html#message
print("Guild ID: ", message.guild.id)
print("Channel ID: ", message.channel.id)
print("User ID: ", message.author.id)
# Unique set of properties for each game instance
gameInstance = (message.guild.id, message.channel.id, message.author.id)
# Bot can't talk to itself
if message.author == client.user:
return
# Detect if user wants to play game
if message.content == "!game":
# Is user already playing game?
if (gameInstance) in players:
await message.channel.send("You are already playing game friend.")
return
# Generate random number
randomNumber = random.randint(1, 10)
# Save random number in dictionary against that game instance
players[gameInstance] = randomNumber
# Ask user for guess
await message.channel.send("You want to play game?! Please enter a number between 1 and 10.")
return
# We only get here if user not entering "!game"
if gameInstance in players:
# Check if message only contains digits then convert it to integer and check guess
# If match, we delete the game instance.
if message.content.isdigit():
guess = int(message.content)
if guess == players[gameInstance]:
del players[gameInstance]
await message.channel.send("You Win!")
else:
await message.channel.send("Try, try, try, try, try again.")
else:
await message.channel.send("That's not a number.")
token = "YOUR TOKEN"
client.run(token)