Discord bot - old posts with old nicknames - python

I've prepared a discord bot that needs to post in name of other users, so I want it to change its nickname. I thought it went ok, but after I restart a client all posts changed to last nickname and no longer be recognizable by author.
Any ideas how to solve it?
Once I saw a discord bot (https://imgur.com/a/eMsmIB3) that posts tweets and changes the name depending on whom tweets it. So I guess it is possible.

So you could do something like this
import discord
import random
import asyncio
from discord.ext.commands import Bot
client = Bot(command_prefix='!', case_insensitive=True)
#client.command(pass_context=True)
async def nick(ctx):
await client.change_nickname(ctx.message.server.get_member("bot_id"),f"Name #{random.randrange(30)}")
embed = discord.Embed()
await asyncio.sleep(.01)
embed.add_field(name = "Poster: ",value = ctx.message.server.get_member("bot_id").nick)
await client.send_message(ctx.message.channel,embed = embed)
I'm not sure if you've tried this but the way that Twitter bot does it also resets its nickname to your client whenever you restart Discord
He also inserts the name of whoever is tweeting into the embed which lasts even after you restart the client.

Related

Discord bot, The bot does not send messages about the arrival of a new member Python

I am new to creating bots for Discord and I take codes from different examples and I write on replit.
I want the bot to send a welcome message to a new member of the server, but when I invite my 2nd account, it doesn't send anything. I have included Intentions
Full code
import discord
from discord import Game
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
from webserver import keep_alive
import os
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
client = commands.Bot(command_prefix='!')
#client.event
async def on_member_join(member):
channel = client.get_channel(channel_id=870695778593701939)
embed = discord.Embed(
title=f"Welcome {member.name}",
description=f"Thanks for joining {member.guild.name}!") # F-Strings!
embed.set_thumbnail(
url=member.avatar_url
) # Set the embed's thumbnail to the member's avatar image!
await channel.send(embed=embed)
And that's all I'm writing on the phone and I don't understand how I can return this whole code and where, so sorry
I'm guessing I have included Intentions means that you have added the intents in the bot constructor and the developers portal
so first
channel = client.get_channel(channel_id=870695778593701939)
channel_id is not the argument name its id and its a positional argument so you can just do client.get_channel(channel_id_here) would work, just replace channel_id_here with the channel id
second when your setting url=member.avatar_url in the set_thumbnail function it would raise an error if the member has no avatar so i would suggest you check if the user has a avatar, if they don't just give it member.default_avatar_url, but if your using discord.py 2.0 (master branch) then its ALOT easier with member.display_avatar.url
this is just a suggestion because display_avatar returns the users/members avatar if they have one or the default discord gives them so no errors are raised

How do I send a message to a specific discord channel while in a task loop?

import discord
from discord.ext import commands, tasks
from discord_webhook import DiscordWebhook
client = discord.Client()
bot = commands.Bot(command_prefix="$")
#tasks.loop(seconds=15.0)
async def getAlert():
#do work here
channel = bot.get_channel(channel_id_as_int)
await channel.send("TESTING")
getAlert.start()
bot.run(token)
When I print "channel", I am getting "None" and the program crashes saying "AttributeError: 'NoneType' object has no attribute 'send' ".
My guess is that I am getting the channel before it is available, but I am unsure. Anybody know how I can get this to send a message to a specific channel?
Your bot cannot get the channel straight away, especially if it's not in the bot's cache. Instead, I would recommend to get the server id, make the bot get the server from the id, then get the channel from that server. Do view the revised code below.
#tasks.loop(seconds=15.0)
async def getAlert():
#do work here
guild = bot.get_guild(server_id_as_int)
channel = guild.get_channel(channel_id_as_int)
await channel.send("TESTING")
(Edit: Including answer from comments so others may refer to this)
You should also ensure that your getAlert.start() is in an on_ready() event, as the bot needs to start and enter discord before it would be able to access any guilds or channels.
#bot.event
async def on_ready():
getAlert.start()
print("Ready!")
Helpful links:
discord.ext.tasks.loop - discord.py docs
on_ready event - discord.py docs
bot.get_guild(id) - discord.py docs
guild.get_channel(id) - discord.py docs
"I can't get certain guild with discord.py" - Stackoverflow

Discord Embed image not working in discord.py

I'm making a bot with discord.py and I want to send the mentioned user's avatar with a glass filter.. This is my code
import aiohttp
import json
import requests
bot = commands.Bot(command_prefix=']')
#glass filter
#bot.command()
async def glass (ctx,*,person:discord.Member =None) :
member = ctx.message.author
person: discord.Member
embed = discord.Embed ()
mentiondp = member.avatar_url
embed.set_image (url = f'https://some-random-api.ml/canvas/glass?avatar={mentiondp}' )
await ctx.send (embed=embed)
But when I tried ]glass #DankMemer It sends a blank embed..
Please Help me!
Currently Discord embeds are down. You'll need to wait a bit until they fix it.
https://discordstatus.com

How to link a discord channel with python

So I am working on making a discord bot for a support server of my friends the one thing i am stuck up on is I want the bot to tell a user to go to a specific channel with the #channel that way they can click on it for convince I have tried some things but I am relatively new to coding any ideas. this is all that i have so far.
import discord
import os
import keep_alive
if message.content.startswith("!help"):
await message.channel.send("To contact a support person go to")
First off you should use a bot command decorator rather than looking for the command statement in the messages content.
In this command you should extract the target channel object through its ID, and mention that channel in your message. In this way users will be able to click on the mention and be redirected to that channel.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def help(ctx):
guild = ctx.guild # The guild to which the channel belongs to
target_channel = guild.get_channel(channel_ID_goes_here)
await ctx.send(f"To contact a support person go to {target_channel.mention}")

Why is my discord bot spamming?

I have previously posted on this same bot and got it working thanks to the people who responded. But, while it finally came to life and turned on, it started spamming messages for no apparent reason. I've looked over the code for typos and can't find any.
Here's the code:
import discord
from discord.ext.commands import bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot (command_prefix = discord)
#client.event
async def on_ready() :
print("Bepis machine fixed")
#client.event
async def on_message(message) :
if message.content == "bepis" :
await client.send_message (message.channel, "bepis")
client.run("Censored Bot Token")
after #client.event is where i need help. also the bottom line if fine this time! turns out i had hit the space bar before the parenthesis and it didn't like that. Help is very appreciated so i can continue adding on to this awesome bot.
It looks like you are sending a message "bepis" in response to the first, then every, message "bepis" - presumably your first response will appear as an entry on the incoming feed which will trigger a second, etc.
Turns out I was not using proper formatting for my bot.
Whenever you say "bepis" in the discord server, the bot will see it and then say "bepis" back, as its intended to do, but, because of my improper formatting, the bot saw itself say "bepis" and responded as if someone else was saying "bepis".
Old lines:
if message.content == "bepis" :
await client.send_message(message.channel, "bepis")
New lines:
if message.content.startswith('bepis'):
await client.send_message(message.channel, "bepis")
So make sure you're using the right format if you're making a bot!
You seemed to already know what the problem is, from this answer you posted. But your solution is far from being able to solve the problem.
on_message is called whenever a new message is sent to anywhere accessible by the bot; therefore, when you type in "bepis" in discord, the bot replies with "bepis", then the message sent by the bot goes into on_message, which the bot re-replies "bepis", and so on...
The simple solution is to check if the message's author is any bot account, or if you want, if the message's author is your bot.
from discord.ext import commands
client = commands.Bot(command_prefix=None)
#client.event
async def on_ready():
print("Bepis machine fixed")
#client.event
async def on_message(message):
# or `if message.author.bot:` # which checks for any bot account
if message.author == client.user:
return
if message.content == "bepis":
await client.send_message(message.channel, "bepis")
client.run("Token")
Note: I also fixed many of your other problems, such as multiple unused imports, another Client, and indentation.
And FYI, command_prefix is only used when command is being processed by a functional command. When you use on_message it has no use, which means you can set it to None.

Categories