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)
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')
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')
I'm building a Discord bot on Python and have an issue in code.
Here's my entire code:
import discord
from discord import message
from discord.ext import commands
client = commands.Bot(command_prefix='_')
gret_words = ['hi', 'grets', 'greetings', 'mornin', 'hey']
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command(pass_context=True)
async def hello(ctx):
author = ctx.message.author
await ctx.send(f'Hello, {author.mention}! My name is Bot-3P0!')
async def on_message(message):
msg = message.content.lower()
if msg in gret_words:
await message.channel.send("Nice to see you!")
####################
client.run('TOKEN')
But my issue is that, when I type in messenger one word from the gret_words list, the bot literally doesn't react! I'll be grateful for all help!
You need to mark on_message as an event. Simply add #client.event on top of async def on_message(message) and it should work! Edit: you will need to add client.process_commands() to your on_message() as well
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
As described in the title, I am new to Python(programming in general) and I tried making a bot, however the bot does not respond to commands. I followed/looked through multiple youtube tutorials & articles, but I cannot find a way to fix my problem.
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.event
async def on_message(message):
if message.author == bot.user:
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
Hey why don't you try this instead the same thing but i removed the on message
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
This should work as when using cogs and when you have a command there is no need to put it in the on_message event. i suggest you watch this series(Really helpful while starting out):
https://www.youtube.com/watch?v=yrHbGhem6I4&list=UUR-zOCvDCayyYy1flR5qaAg