I am trying to make a discord bot to make random encounters for a dnd, Right now I have the code
import discord
import os
import random
import asyncio
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('$desert'):
DesertList = ['charmander', 'charmeleon', 'charizard']
Desert = random.choice(DesertList)
await message.channel.send(Desert)
client.run("just trust me its the right ID")
What I am trying to do is send Desert (defined Desert = random.choice(DesertList)) but the bot doesn't send anything. I have tested if Desert has a value in the console in visual studio and it returns a random name. I was basing it off of something that works in discord where I type $hello in a channel and it replies Hello!
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
K so I finished it, it all works.
The final code is looking like
import discord
import os
import random
import asyncio
client = discord.Client()
DesertList1 = 'none'
DesertList2 = 'none'
def printdesert():
print(DesertList1)
def randomizedesert():
global DesertList1
global DesertList2
DesertList2 = ['charmander', 'charmeleon', 'charizard']
DesertList1 = random.choice(DesertList2)
#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('$desert'):
global DesertList1
randomizedesert()
printdesert()
message.channel.send("bruh")
await message.channel.send(DesertList1)
client.run(":)")
Simplifying it is a necessity but one for a later date
Related
Im trying to make a discord bot, and I want its first command to be pretty simple, I just want it to respond "pong!" when someone types "!ping" but when I try to run the command, nothing happens.. I've tried using other commands too but they all result in errors.. I'm a beginner to using Python, so please, can someone tell me what's wrong with my code, and how I can fix it?
In short, my bot doesn't respond back when I type the command I made for it to do so.
import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#bot.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
Instead of #bot.command you should use #client.command.
Also no need for the from discord.ext.commands import Bot import
So your new code would be
import discord
import os
from discord.ext import commands
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#client.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
You could just do inside on_message code block
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run('your token here')
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('your token here')
As mentioned above, I believe the issues come with the second async function or below, I tried testing it but there was no hello back. Thank you.
You ideally shouldn't be using discord.Client() here but discord.ext.commands.Bot()
Re-write :
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
#bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
#bot.command()
async def hello(ctx):
await ctx.send("Hello!")
bot.run(os.getenv("DISCORD_TOKEN"))
Which would result in :
Also, I can't see any issues in the code you provided other than the second if statement being incorrectly indented but even if that were the case you'll get an error.
Am trying to make simple python bot to read RPG bot and when there is group event it would send text to call everyone to come. Am using Replit so I would say the latest python 3.8.2
import requests
import discord
import time
client = discord.Client()
async def on_ready():
print('Logged in As {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
msg = message.content
if message.content.startswith('Event'):
time.sleep(2)
await message.channel.send("Event Starting #everyone")
if any(word in msg for word in ('event')):
time.sleep(2)
await message.channel.send("#everyone Event time")
client.run(os.environ['token'])
Try to use discord rewrite if you are not use that https://github.com/Rapptz/discord.py
Small example with using your code:
import discord
from discord.ext import commands
import time
import os
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())
bot.remove_command("help")
token = "token here"
#bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
#bot.event
async def on_message(msg):
await bot.process_commands(msg)
if msg.author == bot.user or msg.author.bot:
return
elif "event" in msg.content.lower(): #here we are lower all message content and try to finding "event" word in message.
time.sleep(2)
await msg.channel.send("Event Starting #everyone")
return
#if you want to use this as bot command, not as default text.
#bot.command(name="event")
async def event(msg):
time.sleep(2)
await msg.channel.send("Event Starting #everyone")
return
bot.run(token)
I'm creating a simple DM part of a bot and I've tried this method but it keeps giving me an error.
Code:
import discord
from discord.ext import commands
TOKEN = "Token Here"
#client.event
async def on_message(message):
if message.author == client.user:
return
me = await client.get_user_info('Snowflake ID Here')
await client.send_message(me, "Message Here")
client.run(TOKEN)
It keeps giving me this error:
NameError: name 'client' is not defined
This method seems like the user needs to send a message but is there a way to do it without the user needing to send a message.
import discord
from discord.ext import commands
client = disord.Client(intents=discord.Intents.all()) # define client
# could also be commands.Bot()
#client.event
async def on_message(message):
if message.author == client.user:
return
me = client.get_user(1234) # user id, must be int type
await me.send("Message Here") # client.send(me, "message here") doesnt work in discord.py version 1.x
client.run("TOKEN")
Client Example
import discord
class MyClient(discord.Client): # defineding client
async def on_ready(self): # event
print('Logged on as', self.user)
async def on_message(self, message): # event
if message.author == self.user:
return
you = self.get_user(1234)
await you.create_dm() # go to DM
await you.send("Message Here")
client = MyClient()
client.run('token')
Bot Example
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='>') # defineding client
#bot.event
async def on_ready():
print('Logged on as', bot.user)
#bot.event
async def on_message(message):
if message.author == bot.user:
return
you = bot.get_user(1234)
await you.create_dm() # go to DM
await you.send("Message Here")
bot.run('token')
import os
import discord
import random
from prettytable import PrettyTable
from PIL import Image
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("$picture"):
await channel.send(file=discord.File("images.jpg"))
client.run(os.getenv('TOKEN'))
Hello this is my first time using stack overflow I apologize ahead of time if my post has some mistakes,
so I was trying to have my discord upload a picture when entering a "$picture" in the chat box, but I get an red underline at the line that says await channel.send(file=discord.File("images.jpg")) Saying "undefined named 'channel'" I tried looking into it but couldnt find much. Any ideas? thank you.
You need to do message.channel.send() and not channel.send().
So instead of
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$picture"):
await channel.send(file=discord.File("images.jpg"))
You can do
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$picture"):
await message.channel.send(file=discord.File("images.jpg"))