Reputation:
I don't have any code for this function but I have my bot in which I can implement this code into. I'm new to programming.
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')
Upvotes: 0
Views: 1809
Reputation: 1046
As a new programmer I would advise breaking down your problem into logical steps and trying to solve them separately. You should also need define everything exactly, including all assumptions.
Once you've figured this out, attacking the individual pieces is a little easier. And this is where you want to read documentation.
Before you begin any of this though I would suggest reading an overview of programming concepts. Specifically:
Overall System Design
A discord bot acts like a discord user, and thus can be connected to multiple servers (guilds) and channels at once. The event we are using from the API give you a message that could be from any guild or channel.
Singleplayer Verison
Here's the documentation you would need to answer this question:
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
Aaaaaaand here's the code.
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)
Upvotes: 1