I'm trying to make a discord bot respond when someone makes laughing remarks but it infinitely sends gifs whenever someone types anything
code is as follows
import os
import discord
import random
from discord.ext import commands
import keep_alive
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
#bot.event
async def on_ready():
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("AGOP_Bot is in " + str(guild_count) + " guilds.")
#bot.event
async def on_message(message):
if message.content == "AGOP~hello":
await message.channel.send("https://c.tenor.com/tTXwGpHrqUcAAAAC/summoned.gif")
if message.content == "Lmao" or "Lol" or "lmao" or "lol" and message.author.id != bot_id:
response_funny = ["https://c.tenor.com/mUAgLfICUC0AAAAC/i-didnt-get-the-joke-abish-mathew.gif","https://c.tenor.com/zdoxFdx2wZQAAAAd/not-funny-joke.gif","https://i.pinimg.com/originals/f5/53/97/f55397a7de1c82b37d6d62e655a0e915.gif","https://jutsume.com/images2/2022/04/16/is-this-some-peasant-joke-meme.png","https://c.tenor.com/FnASqUdvJH4AAAAC/whats-so-funny-john.gif"]
await message.channel.send(random.choice(response_funny))
bot.run(Bot_Token)
keep_alive.py
import os
import discord
import random
from discord.ext import commands
# import keep_alive
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
...
#bot.event
async def on_message(message):
if message.content == "AGOP~hello":
await message.channel.send("https://c.tenor.com/tTXwGpHrqUcAAAAC/summoned.gif")
if message.content in ("Lmao" or "Lol" or "lmao" or "lol") and message.author.id != bot.user:
response_funny = ["https://c.tenor.com/mUAgLfICUC0AAAAC/i-didnt-get-the-joke-abish-mathew.gif","https://c.tenor.com/zdoxFdx2wZQAAAAd/not-funny-joke.gif","https://i.pinimg.com/originals/f5/53/97/f55397a7de1c82b37d6d62e655a0e915.gif","https://jutsume.com/images2/2022/04/16/is-this-some-peasant-joke-meme.png","https://c.tenor.com/FnASqUdvJH4AAAAC/whats-so-funny-john.gif"]
await message.channel.send(random.choice(response_funny))
bot.run(Bot_Token)
# keep_alive.py
In addition to some formatting, I changed some of the variables for the API calls. I commented out the keep_alive.py as I assume you are using that to keep your code hosted on Repl.it or something, and you can just comment it back in. I was also able to get this code to work with my bot and execute as you want.
Related
I run the code and send "xd", selfbot replies "lmao", but if another user writes "xd", selfbot does not reply "lmao", What do I do to make it respond to any user or bot?
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
#TOKEN
TOKEN = "u token"
client = discord.Client()
b = Bot(command_prefix = "x")
#b.event
async def on_ready():
print("I ready")
#b.event
async def on_message(message):
if message.content == "xd":
await message.channel.send("lmao")
b.run(TOKEN, bot = False)
you cant do on_message for commands anymore so i recomand you do something like this
#b.command()
async def xd(ctx):
await ctx.send("lmao")
self botting is agaist discord TOS
I run this, it connects however when running it will not return anything when it comes to commands. I've tried changing this to the context return method from on every message. Previously only one message would appear, now none with this method even though both commands are the same with slight differences. What is my error on this? Sorry this is literally my first attempt at a discord bot.
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
botToken = os.getenv("DiscBotToken")
discClient = discord.Client()
bot = commands.Bot(command_prefix = "!")
#discClient.event
async def on_ready():
print(f"{discClient.user} is now connected.")
print('Servers connected to:')
for guild in discClient.guilds:
print(guild.name)
#bot.command(name = "about")
async def aboutMSG(ctx):
aboutResp = "msg"
await ctx.send(aboutResp)
#bot.command(name = "test")
async def testMSG(ctx):
testResp = "msg"
await ctx.send(testResp)
discClient.run(botToken)
You heading in the right direction commands.Bot have both event and command no need to use a client just for events.
You should either use discord.Client or commands.Bot not both in your case it is commands.Bot.
Also you are running the client only
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
botToken = os.getenv("DiscBotToken")
bot = commands.Bot(command_prefix = "!")
#bot.event
async def on_ready():
print(f"{discClient.user} is now connected.")
print('Servers connected to:')
for guild in discClient.guilds:
print(guild.name)
#bot.command(name = "about")
async def aboutMSG(ctx):
aboutResp = "msg"
await ctx.send(aboutResp)
#bot.command(name = "test")
async def testMSG(ctx):
testResp = "msg"
await ctx.send(testResp)
bot.run(botToken)
I am currently trying to create my discord bot. sadly, this does not work and I have no Idea why...
import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = '.')
prefix = '.'
#client.event
async def on_ready():
print("I'm ready! {0.user}".format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))
#client.command()
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
await ctx.send("On my way!")
client.run(os.getenv('TOKEN'))
there are NO errors. But no output aswell. I try making it join my vc by writing: .join
channel returns None, because ctx.message.author don't have voice attribute. Also, Client.join_voice_channel is deprecated since v1.0 (read here)
Instead of that, try this:
import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = '.')
prefix = '.'
#client.event
async def on_ready():
print("I'm ready! {0.user}".format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))
#client.command()
async def join(ctx):
channel = ctx.guild.get_member(ctx.author.id).voice.channel # This
await channel.connect()
await ctx.send("On my way!")
client.run(os.getenv('TOKEN'))
Do client.add_command(join). You currently have the function for your command, but you haven't told the bot to recognize it as a command.
I'm trying to take certain roles when bot starting. First i need to take guild but i couldn't achieve it.
takenGuild = client.get_guild(myServerID)
takenGuild returning None
When i try to loop guilds
for guild in client.guilds:
print(guild.id)
it's not printing anything.
My whole code :
import discord
from discord.ext import commands, tasks
from discord.utils import get
client = commands.Bot(command_prefix = '.')
takenGuild = client.get_guild(123123123123123123)
print(takenGuild.id)
for guild in client.guilds:
print(guild)
print(guild.id)
client.run('Token')
You have to wait for the bot to be ready you can use this. FYI bot is more common now after the updates than client
import discord
from discord.ext import commands, tasks
from discord.utils import get
bot = commands.Bot(command_prefix = '.')
#bot.event
async def on_ready():
print("I am running on " + bot.user.name)
print("With the ID: " + bot.user.id)
print('Bot is ready to be used')
# after it is ready do it
takenGuild = bot.get_guild(123123123123123123)
print(takenGuild.id)
for guild in bot.guilds:
print(guild)
print(guild.id)
bot.run('Token')
I'm trying to make a Python Discord Bot that firstly can delete messages within a channel. I wanted it to be Terminator 3 themed so it would start out by the user saying Skynet then the bot asks to activate Y or N? and when the user types Y it would delete all the messages in the channel if the user typed N it would say judgment day is inevitable. any help would be greatly appreciated.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
token = 'Token'
Client = discord.Client()
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print("Skynet Online")
#client.event
async def on_message(message):
if message.content == 'skynet':
await client.send_message(message.channel, 'Execute Y/N?')
#asyncio.coroutine
async def delete_messages(messages):
if message.content == 'Y':
await client.delete_messages()
client.run('Token')
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
#has_permissions(manage_messages=True, read_message_history=True)
#bot_has_permissions(manage_messages=True, read_message_history=True)
async def purge(ctx, limit: int = 100, user: d.Member = None, *, matches: str = None):
"""Purge all messages, optionally from ``user``
or contains ``matches``."""
logger.info('purge', extra={'ctx': ctx})
def check_msg(msg):
if msg.id == ctx.message.id:
return True
if user is not None:
if msg.author.id != user.id:
return False
if matches is not None:
if matches not in msg.content:
return False
return True
deleted = await ctx.channel.purge(limit=limit, check=check_msg)
msg = await ctx.send(i18n(ctx, 'purge', len(deleted)))
await a.sleep(2)
await msg.delete()
That is the command to delete messages