This is what i have so far:
from discord import Embed
import os
import discord
bot=discord.Client()
nichDat=["|","~",".",",","!","pls"]
#bot.event
async def on_message(message):
if message.content != "NothingButABot":
return
for guild in bot.guilds:
print(guild.name)
for channel in guild.text_channels:
if "bot" not in channel.name:
async for message in channel.history(limit=200):
if not message.author.bot:
for dings in nichDat:
if dings not in message.content:
print(message.content)
What it should do: Print every message once that the bot can see if it isnt connected to a bot (That means none of the strings from nichDat is in it or it is not written by a bot.
What it is doing: Printing every message, that was not written by a bot 5 times.
What can i do that it is doing the right stuff?
try replacing
for dings in nichDat:
if dings not in message.content:
print(message.content)
with
if any(dings not in message.content for dings in nichDat):
print(message.content)
Your code is setup as a loop in a way that it checks each of the strings on your nichtDat list individually and if that single string is not in the message it gets printed, so your filter should not be working correctly either.
I just fixed it by making a search function and completely changing how it works.
This is how:
from discord import Embed
import os
import discord
bot=discord.Client()
nichDat=["|","~",".",",","!","pls"]
def searchfor(dings1):
for dings in nichDat:
if dings1.startswith(dings) == False:
break
print(dings1)
#bot.event
async def on_message(message):
if message.content != "NothingButABot":
return
print(message)
await bot.change_presence(activity=discord.Streaming(name='24/7 chatting', url='https://www.youtube.com/watch?v=dQw4w9WgXcQ'))
print("2")
for guild in bot.guilds:
print(guild.name)
for channel in guild.text_channels:
print(channel.name)
if "bot" not in channel.name:
print("Oke")
async for message in channel.history(limit=200):
if not message.author.bot:
searchfor(message.content)
Related
I want my bot to send a message that clears all reactions of the message after 10 seconds. How can I do that? I can't seem to find anything on the internet.
Here's my code:
import discord
from time import sleep
TOKEN = "my-discord-token"
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message == "test":
msg_id = await message.channel.send("Test")
sleep(10)
{What should I put here?}
client.run(TOKEN)
Thanks!!
You should use await asyncio.sleep(10) instead of sleep(10). Discord library depends on asynchronous programming and when you use sleep(10) from the time library it freezes your entire code. This results in stoping your bot from handling other tasks properly.
You have to change if message... to if message.content. Message object contains not only the message text but other information too. You have to use message.content to get only the text part that you need.
And for clearing reactions you can use clear_reactions().
import asyncio # add this import
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == "test": # you have to use `message.content`
msg = await message.channel.send("Test")
await asyncio.sleep(10)
await msg.clear_reactions()
I am making a discord chat bot. I want to set command "How are You?" But my program only choose 'How' . I need some suggestions.TIA
import discord
from discord.ext import commands
client = commands.Bot(command_prefix= '=')
#client.event
async def on_ready():
print('Bot is online')
#client.command()
async def hello(ctx):
await ctx.send('hello')
#client.command()
async def how_are_you(ctx):
await ctx.send('I am good')
client.run('token')
If you want to use a command, i don't think you can register one with spaces in it, however there are a couple of workarounds:
First you can use the first word of your string as the command name and then check for the content of the message:
#client.command()
async def how(ctx):
if ctx.message.content.lower() == "=how are you":
await ctx.send("I'm fine thanks")
else:
pass
The = prefix is needed in the message, replace it when you want to change your prefix.
Or, the second option would be to set up an on_message listener.
#client.event
async def on_message(message):
if message.content.lower().startswith("=how are you"):
await message.channel.send("I'm fine thanks.")
await client.process_commands(message)
Commands not working but events are tried overriding my on_message but that didn't work. When I comment out the second client.event and down client.command works. Any idea of what I could be doing wrong? am I missing something?
import discord
from discord.ext import commands
import random
import time
from datetime import date
client = commands.Bot(command_prefix = '.')
#client = discord.Client()
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
#client.command()
async def clr(ctx, amount=5):
await ctx.channel.purge(limit=amount)
#client.command(aliases =['should', 'will'])
async def _8ball(ctx):
responses =['As i see it, yes.',
'Ask again later.',
'Better not tell you now.',
"Don't count on it",
'Yes!']
await ctx.send(random.choice(responses))
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
Based on the docs (the ones mentioned by moinierer3000 in the comments) as well as other questions on stack (listed below), on_message will stop your commands from working if you do not process the commands.
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
await client.process_commands(message)
Other questions like this:
discord.py #bot.command() not running
Discord.py Commands not working because of a on_message event
Prefixed and non prefix commands are not working together on python discord bot
I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')
So, I'm working on a discord bot. And am using the on_message() event, which works both on private messages and on servers. I want this to only work in private messages and am unsure of how to go about this.
If anyone can help me, that would be great.
import os
import discord
from discord.ext import commands
TOKEN = ''
quotedUsers = []
client = commands.Bot(command_prefix = '/')
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
await client.change_presence(activity=discord.Game(name='with myself.'))
#client.event
async def on_message(message):
await message.author.send("Recieved: " + message.content)
#client.command()
async def search(ctx, *, question):
await ctx.author.send("Searching: " + question)
client.run(TOKEN)
A message guild also doesn't exist in a group DM, so you have to check if the channel you messaging in is a DM. You can use the dm_channel attribute of a user:
#client.event
async def on_message(message):
if message.channel.id == message.author.dm_channel.id: # dm only
# do stuff here #
elif not message.guild: # group dm only
# do stuff here #
else: # server text channel
# do stuff here #
When a DM is received, it won't have a guild, so you'll be able to use that logic like so:
#client.event
async def on_message(message):
# you'll need this because you're also using cmd decorators
await client.process_commands(message)
if not message.guild:
await message.author.send(f"Received: {message.content}")
References:
Bot.process_commands()
Message.guild - it mentions "if applicable", meaning that it'll return None if it's not in a guild, and rather a DM
f-Strings - Python 3.6+
Hi i had found though playing around that when discord sends a message to the bot as a dm in python this will be done using a discord.channel.DMChanneland if the message is done in the text channel discord.channel.TextChannel.
i have proved this by putting in a type fuct over the object to see what happens in the Message listener.
isinstance(message.channel, DMChannel)